﻿using Bricks.Business.Models.Account;
using Bricks.Business.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils;
using Bricks.Component.Utils.Extensions;
using Bricks.Site.Models.Purview;
using Bricks.Business.Models.BidManagement;
using Bricks.Business.Service.CommonSupport;
using Bricks.Site.Models.BidInfo;

namespace Bricks.Site.Web.Controllers
{
    /// <summary>
    /// 招标管理
    /// </summary>
    [Export]
    public class BidInfoController : BaseController
    {
        [Import]
        public IBidInfoService BidInfoService { get; set; }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ////取得面板及工具栏信息
            //ViewBag.PanelInfo = base.GetPanelInfo();
            ViewBag.ToolBarInfo = base.GetToolBarInfo();
            return View();
        }

        /// <summary>
        /// 获取数据列表/树表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDataList()
        {
            var data = BidInfoService.GetDataList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取招标列表（根据项目ID）
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public ActionResult GetBidsByProjectID(string projectID)
        {
            var data = BidInfoService.GetBidsByProjectID(projectID.ToGuid());
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            BidInfo model = new BidInfo();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = BidInfoService.Bids.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                }
                ViewBag.RegisterUser = UserInfoService.GetUserNameByID(record.RegisterUserID);  //取得用户名
            }
            //如果是新增
            else
            {
                model.RegisterUserID = SystemData.Instance.CurrentUser.ID;
                ViewBag.RegisterUser = SystemData.Instance.CurrentUser.UserName;
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPost(BidInfo model)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var record = BidInfoService.Bids.FirstOrDefault(m => m.ID == model.ID);

            //新增
            if (record == null)
            {
                result = BidInfoService.Add(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //修改
            else
            {
                result = BidInfoService.Modify(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //return View();
            return Json(result, "text/html");
        }

        [HttpPost]
        public ActionResult SaveSectionList(string bidID)
        {
            string data = Request.Form["Data"];
            List<SectionListView> list = JsonHelper.JsonDeserialize<List<SectionListView>>(data);
            OperationResult result = BidInfoService.SaveSectionList(bidID.ToGuid(), list);
            //MenuInfoService.SaveAssign(menuID.ToGuid(), list);
            return Json(result, "text/html");
        }

        /// <summary>
        /// 删除（需关联删除标段）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = BidInfoService.Delete(guidID);
            return Json(exeResult);
        }

        /// <summary>
        /// 获取标段列表（根据招标ID）
        /// </summary>
        /// <param name="bidID"></param>
        /// <returns></returns>
        public ActionResult GetSectionList(string bidID, string correctiveSectionIDs)
        {
            var sectionList = BidInfoService.GetSectionList(bidID.ToGuid());
            if (!String.IsNullOrEmpty(correctiveSectionIDs))
            {
                foreach (var item in sectionList)
                {
                    if (correctiveSectionIDs.Contains(item.ID.ToString()))
                    {
                        item.IsChecked = true;
                    }
                }
            }
            return Json(sectionList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 选择标段
        /// </summary>
        /// <returns></returns>
        public ActionResult ChooseBid()
        {
            return View();
        }

        /// <summary>
        /// 选择标段（Post）
        /// </summary>
        /// <returns></returns>
        public ActionResult ChooseBidPost()
        {
            OperationResult result = new OperationResult(OperationResultType.Success, "选择成功");
            return Json(result);
        }

        [HttpPost]
        /// <summary>
        /// 根据标段ID获取标段名称（格式：项目1-招标1-标段1，项目1-招标2-标段3）
        /// </summary>
        /// <param name="secitonIDs"></param>
        /// <returns></returns>
        public string GetFullSectionNameByID(string secitonIDs)
        {
            string result = string.Empty;
            if (!String.IsNullOrEmpty(secitonIDs))
            {
                string[] ids = secitonIDs.Split(',');
                if (ids != null && ids.Length > 0)
                {
                    List<Guid> guids = new List<Guid>();
                    foreach (string id in ids)
                    {
                        guids.Add(id.ToGuid());
                    }
                    result = BidInfoService.GetFullSectionNameByID(guids);
                }
            }
            return result;
        }

        public ActionResult Preview(string id)
        {
            BidInfo model = new BidInfo();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = BidInfoService.Bids.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                }
                ViewBag.RegisterUser = UserInfoService.GetUserNameByID(record.RegisterUserID);  //取得用户名
            }
            //如果是新增
            else
            {
                model.RegisterUserID = SystemData.Instance.CurrentUser.ID;
                ViewBag.RegisterUser = SystemData.Instance.CurrentUser.UserName;
            }
            return View(model);
        }
    }
}
